//
//  MSMytradeCell.m
//  SocietySale
//
//  Created by Mike on 16/12/8.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSMytradeCell.h"

@interface MSMytradeCell ()
//@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UILabel *stateLabel;
@property (nonatomic, strong) UILabel *tradeNumLabel;
@property (nonatomic, strong) UILabel *nameLabel;

@property (nonatomic, strong) UILabel *themeLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *commentLabel;

@property (nonatomic, strong) UILabel *tradePriceLabel;
@property (nonatomic, strong) UILabel *memberTypeLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIView  *bgView;
@property (nonatomic, strong) UIView *shortHorizonLineView;
@property (nonatomic, strong) UIButton *editBtn;
@property (nonatomic, strong) UIButton *commonBtn;
@end

@implementation MSMytradeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    self.typeLabel = [self creatLabel:CGRectMake(12, 5, SCREEN_WIDTH -40, 25)];
    self.stateLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH -140, self.typeLabel.top+2.5, 60, 20)];
    self.stateLabel.textAlignment = NSTextAlignmentCenter;
    
//    self.editBtn = [self creatBtn:CGRectMake(SCREEN_WIDTH-110, 10, 40, 25)];
//    self.editBtn.layer.borderColor = kBlueColor.CGColor;
//    self.editBtn.layer.borderWidth = 0.8;
//    [self.editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    
    self.commonBtn = [self creatBtn:CGRectMake(SCREEN_WIDTH-70, 10, 55, 25)];
    
    self.tradeNumLabel = [self creatLabel:CGRectMake(self.stateLabel.right+8, self.typeLabel.top, 60, 25)];
    self.tradeNumLabel.textAlignment = NSTextAlignmentRight;
    
    self.nameLabel = [self creatLabel:CGRectMake(12, self.typeLabel.bottom+5, SCREEN_WIDTH-24, 25)];
    
    self.themeLabel = [self creatLabel:CGRectMake(12, self.nameLabel.bottom+5, SCREEN_WIDTH-24, 25)];
    self.contentLabel = [self creatLabel:CGRectMake(12, self.themeLabel.bottom+5, SCREEN_WIDTH-24, 25)];
    self.commentLabel = [self creatLabel:CGRectMake(12, self.contentLabel.bottom+5, SCREEN_WIDTH-24, 25)];
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.commentLabel.bottom + 15 + 20 + 29, SCREEN_WIDTH, 8)]; // top:227
    _bgView.backgroundColor = kDefaultViewBackgroundColor;
    [self.contentView addSubview:_bgView];
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [_bgView addSubview:horizonLineView];
    
    self.tradePriceLabel = [self creatLabel:CGRectMake(12, _bgView.top-49, SCREEN_WIDTH/2, 20)];
    self.memberTypeLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH/2-5, self.tradePriceLabel.top, 130, 20)];
    self.timeLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH-92, self.tradePriceLabel.top, 80, 20)];
    self.timeLabel.textAlignment = NSTextAlignmentRight;
    
    _shortHorizonLineView =[[UIView alloc] initWithFrame:CGRectMake(12, _bgView.top-37, SCREEN_WIDTH-24, 0.6)];// 183+7 190
    _shortHorizonLineView.backgroundColor = kNormalGray;
    [self.contentView addSubview:_shortHorizonLineView];
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(14);
    label.textColor = kDarkGrayColor;
    [self.contentView addSubview:label];
    return label;
}

//创建btn
- (UIButton*)creatBtn:(CGRect)frame{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    btn.titleLabel.font = HeitiFont(14);
    [btn setTitleColor:kBlueColor forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(commonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn];
    return btn;
}

- (void)setModel:(MSTradeModel *)model{
    _model = model;
    self.typeLabel.attributedText = [self getMutableAttriStrWithForStr:@"类型   " backStreet:model.projectType];//;
//    self.stateLabel.text = model.tradeState;
//    self.tradeNumLabel.text = model.tradeNum;
    if ([model.type isEqualToString:@"出售"]) {//只能编辑，不能评价
        if (_model.isComplete) {//可编辑
            self.commonBtn.hidden = NO;
            self.commonBtn.layer.borderColor = kBlueColor.CGColor;
            self.commonBtn.layer.borderWidth = 0.8;
            [self.commonBtn setTitle:@"编辑" forState:UIControlStateNormal];
        }
        else{
            self.commonBtn.hidden = YES;
        }
    }
    else{//购买
        if (!model.isComplete) {//完成
            self.commonBtn.hidden = NO;
            if (!_model.isEvaluate) {//未评价过
                self.commonBtn.layer.borderColor = kBlueColor.CGColor;
                self.commonBtn.layer.borderWidth = 0.8;
                [self.commonBtn setTitle:@"评价" forState:UIControlStateNormal];
            }
            else{
                self.commonBtn.layer.borderColor = [UIColor whiteColor].CGColor;
                self.commonBtn.layer.borderWidth = 0.01;
                [self.commonBtn setTitle:@"已评价" forState:UIControlStateNormal];
            }
        }
        else{//未完成
            if (model.isSelf) {//自己
                self.commonBtn.hidden = NO;
                self.commonBtn.layer.borderColor = kBlueColor.CGColor;
                self.commonBtn.layer.borderWidth = 0.8;
                [self.commonBtn setTitle:@"编辑" forState:UIControlStateNormal];
            }
            else{//他人
                self.commonBtn.hidden = YES;
            }
        }
    }
    
    CGSize typeSize = [HDUtil getSizeWithText:[NSString stringWithFormat:@"类型   %@",model.projectType] font:HeitiFont(14)];
    self.stateLabel.left = MIN(typeSize.width + 24, SCREEN_WIDTH -135);
    
    self.tradeNumLabel.left = self.stateLabel.right + 4;
    
    if ([model.tradeState isEqualToString:@"1"]) {
        self.stateLabel.text = @"交易中";
        self.stateLabel.textColor = UIColorFromRGB(0x3bb25b);
        self.stateLabel.backgroundColor = UIColorHexA(0x3bb25b, 0.1);
    }
    if ([model.tradeState isEqualToString:@"2"]) {
        self.stateLabel.text = @"公开显示";
        self.stateLabel.textColor = kBlueColor;
        self.stateLabel.backgroundColor = UIColorHexA(0x3b99dc, 0.1);
    }
    if ([model.tradeState isEqualToString:@"3"]) {
        self.stateLabel.text = @"已过期";
        self.stateLabel.textColor = kDarkGrayColor;
        self.stateLabel.backgroundColor = kDefaultViewBackgroundColor;
    }
    
    CGSize size = [HDUtil getSizeWithText:self.stateLabel.text font:HeitiFont(14)];
    self.stateLabel.width = size.width + 10;

    NSString * haveName = model.name.length?@"y":@"n";
    NSString * haveTheme = model.theme.length?@"y":@"n";
    NSString * haveContent = model.content.length?@"y":@"n";
    
    NSString * haveTradeEvaluate = @"";
    if (model.tradeEvaluate.length && model.isEvaluate) {//有评价
        haveTradeEvaluate = @"y";
    }
    else{
        haveTradeEvaluate = @"n";
    }
    
    NSMutableArray *haveArray = [NSMutableArray arrayWithCapacity:0];
    NSArray *stateArray = @[haveName,haveTheme,haveContent,haveTradeEvaluate];
    for (NSString *stateStr in stateArray) {
        if ([stateStr isEqualToString:@"y"]) {
            [haveArray addObject:stateStr];
        }
    }
    float height = 30+haveArray.count*30+44+13;
    
    _shortHorizonLineView.top = height-50;
    _bgView.top = height-8;
    self.tradePriceLabel.top = _shortHorizonLineView.top + 10;
    self.memberTypeLabel.top = self.tradePriceLabel.top;
    self.timeLabel.top = self.tradePriceLabel.top;
    
    if (model.name.length) {//有姓名
        self.nameLabel.hidden = NO;
        self.nameLabel.top = self.typeLabel.bottom + 5;
        if (model.theme.length) {//有主题
            self.themeLabel.hidden = NO;
            self.themeLabel.top = self.nameLabel.bottom + 5;
        }
        else{//无主题
            self.themeLabel.hidden = YES;
        }
        self.nameLabel.attributedText = [self getMutableAttriStrWithForStr:@"姓名   " backStreet:model.name];
        if (model.theme.length) {
            self.themeLabel.attributedText = [self getMutableAttriStrWithForStr:@"职务   " backStreet:model.position];
        }
    }
    else{//无姓名
        self.nameLabel.hidden = YES;
        if (model.theme.length) {//有主题
            self.themeLabel.hidden = NO;
            self.themeLabel.top = self.typeLabel.bottom + 5;
        }
        else{//无主题
            self.themeLabel.hidden = YES;
        }
        if (model.theme.length) {
            self.themeLabel.attributedText = [self getMutableAttriStrWithForStr:@"主题   " backStreet:model.theme];
        }
    }
    
//    if (model.theme.length) {
//        self.themeLabel.attributedText = [self getMutableAttriStrWithForStr:@"主题   " backStreet:model.theme];
//    }
    if (model.content.length) {
        self.contentLabel.attributedText = [self getMutableAttriStrWithForStr:@"内容   " backStreet:model.content];
    }
    
    if (model.tradeEvaluate.length && model.isEvaluate) {//有评价
        self.commentLabel.hidden = NO;
        self.commentLabel.top = _shortHorizonLineView.top-33;
        if (model.content.length) {//有内容
            self.contentLabel.hidden = NO;
            self.contentLabel.top = self.commentLabel.top-30;
        }
        else{//无内容
            self.contentLabel.hidden = YES;
        }
        NSMutableAttributedString *commentAttstr = [[NSMutableAttributedString alloc] initWithString:model.tradeEvaluate];
        
        NSTextAttachment *textAttachment0 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        textAttachment0.bounds = CGRectMake(0, 0, 10, 10);
        textAttachment0.image = [UIImage imageNamed:@"star1"];
        NSAttributedString *attachmentString0 = [NSAttributedString attributedStringWithAttachment:textAttachment0];
        
        NSTextAttachment *textAttachment3 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        textAttachment3.bounds = CGRectMake(0, 0, 10, 10);
        textAttachment3.image = [UIImage imageNamed:@"star2"];
        NSAttributedString *attachmentString3 = [NSAttributedString attributedStringWithAttachment:textAttachment3];
        
        [commentAttstr insertAttributedString:attachmentString0 atIndex:5];
        [commentAttstr insertAttributedString:attachmentString0 atIndex:7];
        [commentAttstr insertAttributedString:attachmentString0 atIndex:9];
        [commentAttstr insertAttributedString:attachmentString3 atIndex:11];
        
        self.commentLabel.attributedText = commentAttstr;
    }
    else{//无评价
        self.commentLabel.hidden = YES;
        if (model.content.length) {//有内容
            self.contentLabel.hidden = NO;
            self.contentLabel.top = _shortHorizonLineView.top-33;
        }
        else{//无内容
            self.contentLabel.hidden = YES;
        }
    }
    
    CGSize priceSize = [HDUtil getSizeWithText:model.tradePrice font:[UIFont systemFontOfSize:15]];
    self.memberTypeLabel.left = 115+priceSize.width;
    
    NSMutableAttributedString *priceAttstr = [[NSMutableAttributedString alloc] initWithString:@"交易价格     x" attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment.bounds = CGRectMake(0, -4, 17, 17);
    textAttachment.image = [UIImage imageNamed:@"coin"];
    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [priceAttstr insertAttributedString:attachmentString atIndex:8];
    
    NSAttributedString *priStr = [[NSAttributedString alloc] initWithString:model.tradePrice attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blackColor]}];
    [priceAttstr appendAttributedString:priStr];
    self.tradePriceLabel.attributedText = priceAttstr;
    
    NSString *memberTypeStr = [NSString stringWithFormat:@"[%@]",model.memberType];
    NSMutableAttributedString *memberAttstr = [[NSMutableAttributedString alloc] initWithString:memberTypeStr attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    if ([model.memberLevel integerValue] >0) {
        NSTextAttachment *memberAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        memberAttachment.bounds = CGRectMake(0, -2.5, 15, 12.5);
        memberAttachment.image = [UIImage imageNamed:@"diamond"];
        NSAttributedString *memberAttachmentStr = [NSAttributedString attributedStringWithAttachment:memberAttachment];
        
        for (int i = 0; i<[model.memberLevel integerValue]; i++) {
            [memberAttstr insertAttributedString:memberAttachmentStr atIndex:memberTypeStr.length+i];
        }
    }
    self.memberTypeLabel.attributedText = memberAttstr;

    NSMutableAttributedString *timeAttstr = [[NSMutableAttributedString alloc] initWithString:model.time attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSTextAttachment *textAttachment2 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment2.bounds = CGRectMake(0, -2.5, 14, 14);
    textAttachment2.image = [UIImage imageNamed:@"time"];
    NSAttributedString *attachmentString2 = [NSAttributedString attributedStringWithAttachment:textAttachment2];
    [timeAttstr insertAttributedString:attachmentString2 atIndex:0];
    
    self.timeLabel.attributedText = timeAttstr;
}

- (void)commonClick{
    if ([_model.type isEqualToString:@"出售"]) {//只能编辑，不能评价
        if (_model.isComplete) {//可编辑
            if ([self.delegate respondsToSelector:@selector(clickCommonBtnIsEdit:orderId:)]) {
                [self.delegate clickCommonBtnIsEdit:YES orderId:_model.itemId];
            }
        }
        else{
        }
    }
    else{//购买
        if (!_model.isComplete) {//完成,不能编辑
            if (!_model.isEvaluate) {//未评价过
                if ([self.delegate respondsToSelector:@selector(clickCommonBtnIsEdit:orderId:)]) {
                    [self.delegate clickCommonBtnIsEdit:NO orderId:_model.itemId];
                }
            }
            else{
            }
        }
        else{//未完成，可编辑
            if (_model.isSelf) {//自己
                if ([self.delegate respondsToSelector:@selector(clickCommonBtnIsEdit:orderId:)]) {
                    [self.delegate clickCommonBtnIsEdit:YES orderId:_model.itemId];
                }
            }
            else{//他人
            }
        }
    }
}

- (NSMutableAttributedString*)getMutableAttriStrWithForStr:(NSString*)forStr backStreet:(NSString*)backStr{
    NSMutableAttributedString *AttStr = [[NSMutableAttributedString alloc] initWithString:forStr attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSAttributedString *backAttStr = [[NSAttributedString alloc] initWithString:backStr attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:[UIColor blackColor]}];
    [AttStr appendAttributedString:backAttStr];
    
    return AttStr;
}

+ (CGFloat)cellHeightWithModel:(MSTradeModel*)model{//  +72
    NSString * haveName = model.name.length?@"y":@"n";
    NSString * haveTheme = model.theme.length?@"y":@"n";
    NSString * haveContent = model.content.length?@"y":@"n";
    
    NSString * haveTradeEvaluate = @"";
    if (model.tradeEvaluate.length && model.isEvaluate) {//有评价
        haveTradeEvaluate = @"y";
    }
    else{
        haveTradeEvaluate = @"n";
    }
    
    NSMutableArray *haveArray = [NSMutableArray arrayWithCapacity:0];
    NSArray *stateArray = @[haveName,haveTheme,haveContent,haveTradeEvaluate];
    for (NSString *stateStr in stateArray) {
        if ([stateStr isEqualToString:@"y"]) {
            [haveArray addObject:stateStr];
        }
    }
    float height = 30+haveArray.count*30+44+13;
    
    return height;//235
}

@end

